<?php

/**
 * SearchController: searching Blog, News and Products
 */
class SearchController extends CustomControllerBase {
	
	/**
	 * 
	 * System Category Data Service Object
	 * @var SystemCategoryDataService object
	 */
	public $systemCategoryDataService = null;
	
	/**
	 * Constructor of index
	 */
	public function __construct() {
		parent::__construct ();
		
		$this->systemCategoryDataService = new SystemCategoryDataService ();
		
		// Get Currency of the Owner
		$userCurrencySettingsEntity = $this->settingsDataService->getItem ( SETTINGS_USER_CURRENCY, $this->ownerId );
		$this->currencyCode = $userCurrencySettingsEntity->value;
		$this->currencySymbol = $this->settingsDataService->getCurrencySymbol ( $this->currencyCode );
		
		$this->smarty->assign ( 'currencyCode', $this->currencyCode );
		$this->smarty->assign ( 'currencySymbol', $this->currencySymbol );
	}
	
	/**
	 * The "index" action is the default action for all controllers -- the
	 * landing page of the site.
	 *
	 * @return void
	 */
	public function indexAction() {
		// Set page name
		$this->setPageName ( 'search' );
		
		$module = $this->getPost ( 'module', "" );
		$name = $this->getPost ( 'name', "" );
		
		$arrElement = array ();
		$arrElement ['status'] = UserBlogEntity::ACTIVE;
		switch ($module) {
			case "Blog" :
				$arrElement ['ownerId'] = $this->ownerId;
				$arrElement [] = " title LIKE '%{$name}%'";
				$itemsList = $this->userBlogDataService->getItems ( $arrElement );
				$this->smarty->assign ( 'blogItemList', $itemsList );
				$this->smarty->assign ( 'numberBlogItemList', count ( $itemsList ) );
				
				$arrElement = array ();
				$arrElement ['ownerId'] = $this->ownerId;
				$arrElement ['type'] = CATEGORY_BLOG;
				$catList = $this->systemCategoryDataService->getItems ( $arrElement );
				$this->smarty->assign ( 'catList', $catList );
				$this->smarty->assign ( 'catListCount', count ( $catList ) );
				
				if ($this->isMobileMode == false) {
					// Assign Smarty Content
					$this->assignSmartyContent ( 'Search/blog_search.tpl' );
				} else {
					$this->assignSmartyMobileContent ( 'Search/m_blog_search.tpl' );
				}
				//$this->smarty->assign('SMARTY_CONTENT_PATH', 'template1/Search/blog_search.tpl');
				break;
			case "News" :
				$arrElement ['ownerId'] = $this->ownerId;
				$arrElement [] = " title LIKE '%{$name}%'";
				$itemsList = $this->userNewsDataService->getItems ( $arrElement );
				$this->smarty->assign ( 'newsItemList', $itemsList );
				$this->smarty->assign ( 'numberNewsItemList', count ( $itemsList ) );
				
				$arrElement = array ();
				$arrElement ['ownerId'] = $this->ownerId;
				$arrElement ['type'] = CATEGORY_NEWS;
				$catList = $this->systemCategoryDataService->getItems ( $arrElement );
				$this->smarty->assign ( 'catList', $catList );
				$this->smarty->assign ( 'catListCount', count ( $catList ) );
				if ($this->isMobileMode == false) {
					// Assign Smarty Content
					$this->assignSmartyContent ( 'Search/news_search.tpl' );
				} else {
					$this->assignSmartyMobileContent ( 'Search/m_news_search.tpl' );
				}
				//$this->smarty->assign('SMARTY_CONTENT_PATH', 'template1/Search/news_search.tpl');
				break;
			case "Product" :
				$arrElement ['ownerId'] = $this->ownerId;
				$arrElement [] = " user_product_description.name LIKE '%{$name}%'";
				$itemsList = $this->userProductDataService->searchItems ( $arrElement );
				$this->smarty->assign ( 'productItemList', $itemsList );
				$this->smarty->assign ( 'numberProductItemList', count ( $itemsList ) );
				
				if ($this->isMobileMode == false) {
					// Assign Smarty Content
					$this->assignSmartyContent ( 'Search/product_search.tpl' );
				} else {
					
					$this->assignSmartyMobileContent ( 'Search/m_product_search.tpl' );
				}
				//$this->smarty->assign('SMARTY_CONTENT_PATH', 'template1/Search/product_search.tpl');
				break;
			default :
				if ($this->isMobileMode == false) {
					// Assign Smarty Content
					$this->assignSmartyContent ( 'Search/default_search.tpl' );
				} else {
					$this->assignSmartyMobileContent ( 'Search/m_default_search.tpl' );
				}
				//$this->smarty->assign('SMARTY_CONTENT_PATH', 'template1/Search/default_search.tpl');
				break;
		}
		
		$this->setModuleName ( $module );
	
		// Get list category
	

	//$this->smarty->display(APPLICATION_PATH . '/Layouts/template1/MainLayout.tpl');
	}

}
